<template>
  <view class="content">
    <image class="logo" src="/static/logo.png"></image>
    <view class="text-area">
      <text class="title">{{ title }}</text>
    </view>

    用户名：<input type="text" v-model="userName"/>
    密码：<input type="password" v-model="password"/>

    <button @click="login()">登陆</button>

    <button @click="getCurrentUserInfo()">获取当前登陆用户的信息</button>

    <view>
      <button type="primary" @click="scanCode()">扫码登陆</button>
    </view>
  </view>
</template>

<script>

export default {
  data() {
    return {
      title: '我要扫码',
      userName: 'luo',
      password: '1234',
      serverAddr: 'http://1t61393v82.51vip.biz',//'http://192.168.1.140:9090'
      // request:null,
    }
  },
  onLoad() {

    //   this.request = axios.create({
    //     // 【开发时候开启】API 请求的默认前缀
    //     //baseURL: process.env.VUE_APP_API_BASE_URL,
    //     //【打包时候开启】API 请求的默认前缀,npm run build 使用使用,--打包之后纯js,没有node环境，无法代理
    //     baseURL: "http://192.168.1.140:9090",
    //     timeout: 1440000, // 请求超时时间-文件下载

    //   })

    //   //请求拦截器
    //   this.request.interceptors.request.use(config => {


    //     config.headers['auth_token'] = uni.getStorageSync("auth_token")// 让每个请求携带自定义token 请根据实际情况自行修改

    //     return config
    //   })
  },
  methods: {

    login() {

      uni.request({
        url: `${this.serverAddr}/login?password=${this.password}&userName=${this.userName}`,
        method: 'post',
        success: (resp) => {


          if (resp.data.code === 200) {

            uni.setStorage({
              key: 'auth_token',
              data: resp.data.auth_token
            });
            uni.showToast({
              title: '登陆成功',
              duration: 2000
            });
          }

        }
      })
    },

    getCurrentUserInfo() {

      uni.request({
            url: this.serverAddr + '/userInfo',
            method: 'get',
            header: {
              auth_token:uni.getStorageSync("auth_token")
            },
            success: resp => {
              if (resp.data.code === 200) {
                uni.showToast({
                  title: '当前登陆：' + resp.data.data.name,
                  duration: 2000
                });


              } else {
                uni.showToast({
                  title: resp.data.msg,
                  duration: 2000
                });
              }
            }
          }
      )

    },

    scanCode() {
      // 只允许通过相机扫码
      uni.scanCode({
        onlyFromCamera: false,
		fail:e=>{
			uni.showToast({
			  title: e.errMsg,
			  duration: 2000
			});
		},
        success: (res) => {
			const auth_token = res.result

          console.log('条码类型：' + res.scanType);
          console.log('条码内容：' + auth_token);
		  

          uni.showModal({
            title: '提示',
            content: '确认登陆？',
            success: res=> {
              if (res.confirm) {
                console.log('用户点击确定');
                uni.request({
                  url: this.serverAddr + "/phoneScanQrCodeLogin?unAuthenticatedJwtToken=" + auth_token,
				  fail:e=>{
					  
				  },
                  success: resp => {

					if(resp.data.code===200){
						uni.showToast({
						  title: '登陆成功：' + resp.data.data.principal.username,
						  duration: 2000
						});
					}else{
						uni.showToast({
						  title:  resp.data.msg,
						  duration: 2000
						});
					}
              
                  },
				  
                })


              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            }
          });


        }
      });
    }

  },
}
</script>

<style>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.logo {
  height: 200 rpx;
  width: 200 rpx;
  margin-top: 200 rpx;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 50 rpx;
}

.text-area {
  display: flex;
  justify-content: center;
}

.title {
  font-size: 36 rpx;
  color: #8f8f94;
}
</style>
